home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_09_12 / 9n12022a < prev    next >
Text File  |  1991-10-13  |  3KB  |  105 lines

  1.  
  2. void idle(unsigned priority)
  3. {
  4.     printf("idle @ %3u\n", priority);
  5. }
  6.     
  7. /* ----------------------------------------------------------- */
  8.  
  9. void pro1(unsigned priority)
  10. {
  11.     printf("pro1 @ %3u\n", priority);
  12. }
  13.  
  14. /* ----------------------------------------------------------- */
  15.  
  16. void pro2(unsigned priority)
  17. {
  18.     printf("pro2 @ %3u\n", priority);
  19. }
  20.  
  21. /* ----------------------------------------------------------- */
  22.  
  23. void pro3(unsigned priority)
  24. {
  25.     printf("pro3 @ %3u\n", priority);
  26. }
  27.  
  28. /* --------------------------------------------------------------
  29.  
  30. FUNCTION PROC_CMD: The steps to process a command are:
  31.  
  32. Note that handlers registered using signal require one int argument. 
  33. When a handler is invoked, the argument passed to it is the type of
  34. signal that caused its invocation. Since, in this case it is always
  35. SIGINT, the argument is never used within this handler.
  36.  
  37. actions is a jump table whose index is the menu option chosen by
  38. the user.
  39.  
  40. A. Since we have just trapped a SIGINT signal, the library will now
  41.    revert to using the SIG_DFL method for future SIGINT signals that
  42.    occur while this signal handler is executing. This default
  43.    method generally results in the program being abnormally
  44.    terminated and that is unsuitable behavior here. As such, we ask
  45.    to have such signals ignored until we are again ready to them.
  46.  
  47. B. Prompt the user for their menu choice.
  48.  
  49. C. Call the corresponding function.
  50.  
  51. D. Reregister this handler for future SIGINT signals.
  52.  
  53. -------------------------------------------------------------- */
  54.  
  55. void proc_cmd(int dummy)
  56. {
  57.     int code;
  58.     static const void (*actions[])(void) = {
  59.         myexit,
  60.         help,
  61.         add_node
  62.  
  63.     };
  64.  
  65. /*A*/    if (signal(SIGINT, SIG_IGN) == SIG_ERR) {
  66.         fprintf(stderr, "can't ignore signal\n");
  67.         exit(EXIT_FAILURE);
  68.     }
  69.  
  70.     while (1) {
  71. /*B*/        printf("\nEnter Action Code (1 for help): ");
  72.         scanf("%d", &code);
  73.  
  74.         if (code < 0 || code >= NUMELEM(actions)) {
  75.             fprintf(stderr, "\n  Invalid command\n");
  76.             continue;
  77.         }
  78.  
  79. /*C*/        (*actions[code])();    /* call selected action */
  80.         break;
  81.     }
  82.  
  83. /*D*/    if (signal(SIGINT, proc_cmd) == SIG_ERR) {
  84.         fprintf(stderr, "can't register handler\n");
  85.         exit(EXIT_FAILURE);
  86.     }
  87. }
  88.  
  89. /* ----------------------------------------------------------- */
  90.  
  91. void myexit(void)
  92. {
  93.     exit(EXIT_SUCCESS);
  94. }
  95.  
  96. /* ----------------------------------------------------------- */
  97.  
  98. void help(void)
  99. {
  100.     printf("\n  The action codes are:\n");
  101.     printf("\t0 - Exit this program\n");
  102.     printf("\t1 - Produce this help message\n");
  103.     printf("\t2 - Add a node\n");
  104. }
  105.